package com.miyake.dsp.osa.lab.wrapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.osa.lab.Calculator;
import com.miyake.dsp.osa.lab.FileSetting;
import com.miyake.dsp.osa.lab.LabUiAnnotation;

@LabUiAnnotation(caption="MS9740A CSV Reader")
public class TextReaderWrapper extends Calculator {
	private static final String OUTPUT = "Output";
	private FileSetting setting;
	
	public TextReaderWrapper(FileSetting setting) {
		this.setting = setting;
		initOutput(OUTPUT);
	}
	
	@Override
	public synchronized void process() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(setting.getFilePath())));
			String line;
			List<Double> d = new LinkedList<Double>();
			while( (line = reader.readLine()) != null) {
				d.add(Double.valueOf(line));
			}
			this.setOutput(OUTPUT, new ComplexArray(ArrayUtils.toPrimitive(d.toArray(new Double[0]))));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Object getSetting() {
		return setting;
	}

}
